<?php
/**
 * 会员自助购买广告位 - 织梦技术研究中心！
 *
 * @version        $Id: buyad.php 14:31:15 2013年10月10日 土匪 $
 * @package        DedeCMS menber ad
 * @copyright      Copyright (c) 2013, Dedejs.com
 * @link           http://bbs.dedejs.com
 */

require_once(dirname(__FILE__)."/config.php");
CheckRank(0,0);
$menutype = 'mydede';
require_once(DEDEINC."/datalistcp.class.php");
setcookie("ENV_GOBACK_URL",$dedeNowurl,time()+3600,"/");

$db->SetQuery("SELECT aid,name FROM #@__memad_type");
$db->Execute();
$options = '';
while($rs = $db->GetArray())
{
$options .= "<option value='".$rs['aid']."'>".$rs['name']."</option>\r\n";
}
/*
function look();
*/
if($action=='look')
{
	$sql = "Select c.*,m.name FROM #@__memad c left join #@__memad_type m on c.tid=m.aid where cid='$cid'";
	$sector = $dsql->GetOne($sql);
	AjaxHead();
?>
<table width="100%" border="0" cellspacing="0" cellpadding="0">

<tr>
<?php if($sector['litpic']=="") {?><a href="http://bbs.dedejs.com" target="_blank"><img src="templets/images/nolitp.jpg" width="300" height="380" border="0" /></a> <?php } else { ?>
<a href="javascript:CloseEditCatalog()"><img src="<?php echo $sector['litpic']; ?>" width="300" height="380" border="0" /></a><?php }?>
</tr>
<tr>
<td width="100%" align="right" valign="top"><a href="javascript:CloseEditCatalog()">[关闭]</a></td>
</tr>
</table>

    
<?php
exit();
}
$memer = $dsql->getone("Select money FROM `#@__member` where mid='$cfg_ml->M_ID' limit 1");
$mid = $dsql->getone("Select mid FROM `#@__member` where mid='$cfg_ml->M_ID' limit 1");
$query = "Select cid,mcid,normbody,normurl,price,adtype,mcid FROM #@__memad  where cid='$cid' limit 1 ";
		
$sector = $dsql->GetOne($query);
$ntime=time();
$normbodys = $normurls = "";
if($action == 'buy')
{
 if(empty($step))
  {
    $morebuy = @floor($memer['money']/$sector['price']);
	if($sector['mcid']!=0 && $sector['mcid']== $cfg_ml->M_ID){
    $normbodys=$sector['normbody'];
	$normurls=$sector['normurl'];
    }
	if($sector['adtype']=='1'){
	$nbody = "<tr><td width=150 height=35 align=right bgcolor=#FFFFFF>文字内容：</td><td bgcolor=#FFFFFF> &nbsp;&nbsp; <input name=normbody type=text style='height:20px;font-size:14px; line-height:23px;' size=50 value='".$normbodys."'></td></tr><tr><td width=150 height=35 align=right bgcolor=#FFFFFF>文字链接：</td><td bgcolor=#FFFFFF> &nbsp;&nbsp; <input name=normurl type=text style='height:20px;font-size:14px; line-height:23px;' size=50 value='".$normurls."'></td></tr>";
	}
    elseif($sector['adtype']=='2'){
    $nbody = "<tr><td width=150 height=35 align=right bgcolor=#FFFFFF>图片地址：</td><td bgcolor=#FFFFFF> &nbsp;&nbsp; <input name=normbody type=text style='height:20px;font-size:14px; line-height:23px;' size=50 value='".$normbodys."'> <input name=pic_bt type=button class=intxt  value=从上传附件中选择 onClick=SelectImage('form2.normbody','big') style='width:150px;height:23px;' /></td></tr><tr><td width=150 height=20 align=right bgcolor=#FFFFFF>图片链接：</td><td bgcolor=#FFFFFF> &nbsp;&nbsp; <input name=normurl type=text style='height:20px;font-size:14px; line-height:23px;' size=50 value='".$normurls."'></td></tr>";
    }
     else{
	$nbody = "<tr><td width=150 height=35 align=right bgcolor=#FFFFFF>Flash地址：</td><td bgcolor=#FFFFFF> &nbsp;&nbsp; <input name=normbody type=text style='height:20px;font-size:14px; line-height:23px;' size=50 value='".$normbodys."'> <input name=pic_bt type=button class=intxt style='width:150px;height:23px;' value=从上传附件中选择 onClick=SelectFlash('form2.normbody','big') /> </td></tr>";
	}
   }
else if($step == 2){
   $endtime = $ntime + 86400 * $times * 7;
   $times = intval($times);
   $mones= $times * $sector['price'];
   if($times<=0 or $times=="")
	{
      ShowMsg('购买时长至少要为1周！','-1');
	  exit();
	 }
   if($memer['money'] < $mones)
	 {
		ShowMsg('您的金币不够','-1');
		exit();
	 }
   if($adtype == 2 && ( (ereg(':',$normbody) && !eregi('^http:',$normbody)) || !eregi("\.(jpg|png|gif)",$normbody)))
	 {
		ShowMsg('你指定的图片网址存在问题！','-1');
		exit();
	 }
    if($adtype == 3 && ( (ereg(':',$normbody) && !eregi('^http:',$normbody)) || !eregi("\.(swf)",$normbody)))
	 {
		ShowMsg('你指定的Flash网址存在问题！','-1');
		exit();
	 }
	else
	 {
       $querys = "update `#@__member` set money=money-$mones where mid='$cfg_ml->M_ID' ";
	   if($dsql->executenonequery($querys)){
		  $query = "update `#@__memad` set mcid='$cfg_ml->M_ID',times='$times',starttime='$ntime',endtime='$endtime',normbody='$normbody',normurl='$normurl' where cid='$cid' ";
		}
		if($dsql->ExecuteNoneQuery($query))
		{
			ShowMsg('购买广告位成功，现在将返回管理页面','buyad.php');
			exit();
		}
		else
		{
			ShowMsg('购买失败，请联系管理员！','buyad.php');
			exit();
		}
	 }
  }
	
	include(DEDEMEMBER."/templets/buyad.htm");
	exit();
}

$wheresql = "WHERE cid>0 And sales<3";
if($tid > 0)
{
	$wheresql .= " AND (tid='".$tid."')";
}

if($sales > 0)
{
	$wheresql .= " AND (sales='".$sales."')";
}
if($type > 0)
{
	$wheresql .= " AND (adtype='".$type."')";
}
if($price1 > 0)
{
	$wheresql .= " AND (price>='".$price1."')";
}
if($price2 > 0)
{
	$wheresql .= " AND (price<='".$price2."')";
}
if($price3 > -1)
{
	$wheresql .= " AND (price<='".$price3."')";
}

$query = "SELECT c.*,m.name,l.mid FROM #@__memad c left join #@__memad_type m on c.tid=m.aid left join #@__member l on c.mcid=l.mid $wheresql order by c.tid,c.lists ";


$dlist = new DataListCP();
$dlist->pageSize = 10;
$dlist->SetParameter("tid",$tid);
$dlist->SetParameter("sales",$sales);
$dlist->SetParameter("type",$type);
$dlist->SetParameter("price1",$price1);
$dlist->SetParameter("price2",$price2);
$dlist->SetParameter("price3",$price3);
//这两句的顺序不能更换
$dlist->SetTemplate(DEDEMEMBER."/templets/buyad.htm");      //载入模板
$dlist->SetSource($query);            //设定查询SQL
$dlist->Display();                  //显示

?>